package com.interview.product.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.interview.core.attachment.entity.Attachment;
import com.interview.core.attachment.service.AttachmentService;
import com.interview.core.page.Page;

import com.interview.product.entity.Product;
import com.interview.product.query.ProductConditionQuery;
import com.interview.product.service.ProductService;

@Controller
@RequestMapping(value = "product")
public class ProductController {

	@Resource
	private ProductService productService;
	@Resource
	private AttachmentService attachmentService;

	@RequestMapping(value = "page")
	public String page(ModelMap map, ProductConditionQuery conditionQuery) {

		Page page = productService.findByConditionQuery(conditionQuery);

		Integer pageIndex = page.getPageIndex();
		Integer pageSize = page.getPageSize();
		Integer total = page.getTotal();
		List<Product> products = page.getList();

		Integer totalPage = (total / pageSize);

		if (total % pageSize != 0) {
			totalPage = totalPage + 1;
		}

		if (totalPage <= 0) {
			totalPage = 1;
		}
		if (pageIndex >= totalPage & totalPage != 0) {
			pageIndex = totalPage - 1;
		}

		map.put("pageIndex", pageIndex);
		map.put("pageSize", pageSize);
		map.put("totalPage", totalPage);
		map.put("list", products);
		map.put("conditionQuery", conditionQuery);

		return "product/page";
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String toAdd(ModelMap map) {
		return "product/add";
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(Product product, ModelMap map) {

		String attachmentId = product.getAttachmentId();

		if (attachmentId=="" || attachmentId.contains(" ")) {
			attachmentId = null;
			product.setAttachmentId(null);
		}

		Product addProduct = productService.save(product);
		if (attachmentId != null) {
			Attachment attachment = attachmentService.findOne(attachmentId);
			attachment.setBizCode(addProduct.getId());
			attachmentService.update(attachment);
		}
		return "redirect:/product/page";
	}

	@RequestMapping(value = "{id}/edit", method = RequestMethod.GET)
	public String toEdit(ModelMap map, @PathVariable String id) {

		String relativeFilePath = null;

		Product product = productService.findOne(id);

		String attachmentId = product.getAttachmentId();

		if (attachmentId != null) {
			Attachment attachment = attachmentService.findOne(attachmentId);

			relativeFilePath = attachment.getRelativeFilePath();

		}

		map.put("RelativePath", relativeFilePath);
		map.put("product", product);
		return "product/edit";

	}

	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public String edit(Product product, ModelMap map) {

		String id = product.getId();
		Product beforProduct = productService.findOne(id);
		String beforAttachmentId = beforProduct.getAttachmentId();
		String attachmentId = product.getAttachmentId();

		if (beforAttachmentId != null & attachmentId != null) {
			if (!beforAttachmentId.equals(attachmentId)) {
				attachmentService.delete(beforAttachmentId);
			}
		}
		if (attachmentId != null) {
			Attachment attachment = attachmentService.findOne(attachmentId);
			attachment.setBizCode(id);
			attachmentService.update(attachment);
		}
		productService.update(product);

		return "redirect:/product/page";
	}

	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	public String delete(ModelMap map, @PathVariable String id) {
		Product product = productService.findOne(id);
		String attachmenId = product.getAttachmentId();
		if (attachmenId != null) {
			attachmentService.deleteFile(attachmenId);
		}

		productService.delet(id);
		return "redirect:/product/page";
	}

	@RequestMapping(value = "{id}/view", method = RequestMethod.GET)
	public String view(@PathVariable String id, ModelMap model) {

		String relativeFilePath = null;
		Product product = productService.findOne(id);
		String attachmentId = product.getAttachmentId();

		if (attachmentId != null) {
			relativeFilePath = attachmentService.findOne(attachmentId)
					.getRelativeFilePath();
		}
		model.put("RelativePath", relativeFilePath);
		model.put("product", product);
		return "product/view";
	}

	@RequestMapping(value = "childpage", method = RequestMethod.GET)
	public String childPage(ModelMap map,ProductConditionQuery conditionQuery){
		page(map, conditionQuery);
		return "product/childpage";
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));// true:允许输入空值，false:不能为空值
	}

	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	public Object productData(@PathVariable String id, ModelMap model) {
		return productService.findOne(id);
	}

	@RequestMapping
	@ResponseBody
	public Object productAllData(ProductConditionQuery conditionQuery) {
		return productService.findByConditionQuery(conditionQuery);
	}
}
